package com.example.studentproj.controller;

import com.example.studentproj.model.Class1;
import com.example.studentproj.model.Student;
import com.example.studentproj.service.ClassService;
import com.example.studentproj.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class StudentController {

    //依赖注入StudentService接口
    @Autowired
    private StudentService studentService;

    //依赖注入ClassService接口
    @Autowired
    private ClassService classService;



    //将获取到的班级名称信息传入model，并且跳转分页函数
    @GetMapping("/")
    public String viewHomePage(Model model) {
        List<Class1> listclass = classService.getAllClass();
        model.addAttribute("listclass",listclass);
        return findPaginated(1, "name", "asc" ,model);
    }


    //跳转添加学生信息网页
    @GetMapping("/showNewStudentForm")
    public String showStudet(Model model){
        Student student = new Student();
        List<Class1> listclass = classService.getAllClass();
        model.addAttribute("listclass",listclass);
        model.addAttribute("student",student);
        return "new_student";
    }

    //跳转保存学生信息网页
    @PostMapping("/saveStudent")
    public String saveStudnt(@ModelAttribute("student") Student student){
        studentService.saveStudent(student);
        return "redirect:/";
    }

    //根据id读取数据库数据，并支持修改
    @GetMapping("/showNewStudentUpdate{id}")
    public String showFormForUpdate(@PathVariable(value = "id") long id,Model model){
        Student student = studentService.getStudentByid(id);
        List<Class1> listclass = classService.getAllClass();
        model.addAttribute("listclass",listclass);
        model.addAttribute("student",student);
        return "update_student";
    }


    //根据id读取数据库数据，并直接删除掉
    @GetMapping("/deleteStudent{id}")
    public String deleteStudent(@PathVariable(value = "id") long id){
        this.studentService.deleteStudentByid(id);
        return "redirect:/";
    }


    //获取分页数据
    @GetMapping("/page/{pageNo}")
    public String findPaginated(@PathVariable (value = "pageNo") int pageNo,
                                @RequestParam("sortField") String sortField,
                                @RequestParam("sortDir") String sortDir,
                                Model model) {
        //设置每4条数据分一页
        int pageSize = 4;

        Page<Student> page = studentService.findPaginated(pageNo, pageSize, sortField, sortDir);
        List<Student> ListStudent = page.getContent();

        model.addAttribute("currentPage", pageNo);
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("totalItems", page.getTotalElements());

        model.addAttribute("sortField", sortField);
        model.addAttribute("sortDir", sortDir);
        model.addAttribute("reverseSortDir", sortDir.equals("asc") ? "desc" : "asc");

        model.addAttribute("listStudent", ListStudent);
        return "index";
    }

    //搜索名字
    @GetMapping("/query{inpname}")
    public String query(@PathVariable(value = "inpname") String inpame,Model model){
        List<Student> listStudent = studentService.findByNameContaining(inpame);
        List<Class1> listclass = classService.getAllClass();
        model.addAttribute("listclass",listclass);
        model.addAttribute("listStudent",listStudent);
        return "index";
    }


    //根据班级名称来查找数据
    @GetMapping("/classname{inpname}")
    public String classname(@PathVariable(value = "inpname") String inpame,Model model){
        List<Student> listStudent = studentService.findByClassName(inpame);
        List<Class1> listclass = classService.getAllClass();
        model.addAttribute("listclass",listclass);
        model.addAttribute("listStudent",listStudent);
        return "index";
    }

    //添加班级映射
    @GetMapping("/addClass")
    public String addClass(Model model) {
        Class1 class1 = new Class1();
        model.addAttribute("class", class1);
        return "add_class";
    }




    //保存班级表
    @PostMapping("/saveClass")
    public String saveClass(@ModelAttribute("class") Class1 class1){
        classService.saveClass(class1);
        return "redirect:classInfo";
    }



    //分页班级表
    @GetMapping("/classInfo")
    public String classInfo(Model model) {
        return findPaginated1(1, "name", "asc" ,model);

    }

    //获取分页数据
    @GetMapping("/page1/{pageNo}")
    public String findPaginated1(@PathVariable (value = "pageNo") int pageNo,
                                 @RequestParam("sortField") String sortField,
                                 @RequestParam("sortDir") String sortDir,
                                 Model model) {
        //设置每4条数据分一页
        int pageSize = 4;

        Page<Class1> page = classService.findPaginated1(pageNo, pageSize, sortField, sortDir);
        List<Class1> listClass = page.getContent();

        model.addAttribute("currentPage1", pageNo);
        model.addAttribute("totalPages1", page.getTotalPages());
        model.addAttribute("totalItems1", page.getTotalElements());

        model.addAttribute("sortField1", sortField);
        model.addAttribute("sortDir1", sortDir);
        model.addAttribute("reverseSortDir1", sortDir.equals("asc") ? "desc" : "asc");

        model.addAttribute("listClass", listClass);
        return "class_info";
    }

    //根据id读取数据库数据，并支持修改
    @GetMapping("/updateClass{id}")
    public String updateClass(@PathVariable(value = "id") long id,Model model){
        Class1 class1 = classService.getClassByid(id);
        model.addAttribute("class1",class1);

        return "update_class";
    }
//    @GetMapping("/updateClass/{id}")
//    public String showUpdateClassForm(@PathVariable(value = "id") long id, Model model) {
//        Class1 class1 = classService.getClassByid(id);
//        model.addAttribute("class1", class1);
//        return "update_class";
//    }

    @PostMapping("/updateClasss/{id}")
    public String updateClass1(@PathVariable(value = "id") long id, @RequestParam("className") String className, @RequestParam("oldClassName") String oldClassName){
        // 获取班级实体
        Class1 class1 = classService.getClassByid(id);

        // 更新班级名称
        class1.setName(className);
        classService.saveClass(class1);

        // 同步更新学生表中对应班级的名称
        List<Student> students = studentService.findByClassName(oldClassName);
        for (Student student : students) {
            student.setClassName(className);
            studentService.saveStudent(student);
        }

        return "redirect:/classInfo"; // 重定向到班级列表页面
    }


    //根据id读取数据库数据，并直接删除掉
    @GetMapping("/deleteClass{id}")
    public String deleteClass(@PathVariable(value = "id") long id){
        classService.deleteClassByid(id);
        return "redirect:classInfo";
    }



    }


